﻿using System;
using CardReader;
using Micropayment.SysMgr.Service.AccountMgrService.Impl;
using Micropayment.SysMgr.Service.Entity;
using Micropayment.WebService;

namespace Micropayment.BOM.Service
{
    public class CardService
    {
        private static CardService instance;
        private Card_Reader cardReader;
        private IntPtr handle;
        private RechargeService rechargeService;
        private CardService()
        {
            rechargeService = new RechargeService();
            cardReader = new Card_Reader();
            handle = this.cardReader.OpenReader("COM3");
        }

        public static CardService GetInstance()
        {
            if (instance == null)
            {
                instance = new CardService();
            }
            return instance;
        }

        public CardInfo GetCardInfo()
        {
            bool flag = true;
            CardInfo cardInfo = new CardInfo();
            IntPtr handle = this.cardReader.OpenReader("COM1");
            if ((int)handle != 0)
            {
                while (flag)
                {
                    if (this.cardReader.ReserchCard(handle) == 0)//寻卡成功
                    {
                        flag = false;
                        //激活卡成功
                        if (this.cardReader.ActvCard(handle) == 0)
                        {
                            //获取卡片应用序列号
                            cardInfo.CardID = this.cardReader.GetCardID(handle);
                            //获取电子钱包余额
                            cardInfo.QbAmount = this.cardReader.GetBalance(handle);
                            //根据卡号查询资金账户金额
                            AccountMgrServiceImpl accountMgrServiceImpl = new AccountMgrServiceImpl();
                            cardInfo.ZjAmount = accountMgrServiceImpl.GetZjAmountByCardID(cardInfo.CardID).ToString();
                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("激活卡失败！", "普通移资");
                        }
                    }
                    else
                    {
                        flag = true;
                        Microsoft.Windows.Controls.MessageBox.Show("寻卡失败！", "普通移资");
                    }
                }
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("打开读卡器失败！", "普通移资");
            }
            //关闭读卡器
            this.cardReader.CloseReader(handle);
            return cardInfo;
        }

        /// <summary>
        /// 进行充值
        /// </summary>
        /// <param name="amount">充值金额</param>
        /// <param name="deviceID">设备ID 长度为12位</param>
        public void Recharge(int amount,string deviceID)
        {
            //string mac2 = this.rechargeService.GetMac2("000000000000", 120, 234, "111111111111", "0B0B0B0B0B0B");
            //string test = this.rechargeService.GetMac2("", 0, 100, "", "");
            string strAppSeq = "";
            string strBalence = "";
            string strMAC1 = "";
            try
            {
                bool flag = true;
                //打开读卡器
                IntPtr handle = this.cardReader.OpenReader("COM1");
                if ((int)handle != 0)
                {
                    while (flag)
                    {
                        if (this.cardReader.ReserchCard(handle) == 0)//寻卡成功
                        {
                            flag = false;
                            //激活卡成功
                            if (this.cardReader.ActvCard(handle) == 0)
                            {
                                //获取卡片应用序列号
                                strAppSeq = this.cardReader.GetCardID(handle);
                                //获取电子钱包余额
                                strBalence = this.cardReader.GetBalance(handle);
                                //显示余额
                                this.cardReader.LEDDisplay(handle, strBalence, 4000);
                                //准备进行充值
                                strMAC1 = this.cardReader.PrepareRecharge(handle, amount, deviceID);
                                //获取卡片联机交易序列号
                                string cardCountID = strMAC1.Substring(8, 4);
                                //取得mac2 
                                string mac2 = this.rechargeService.GetMac2(strAppSeq, amount, Int32.Parse(strBalence), deviceID, strMAC1.Substring(16, 8),0, cardCountID);
                                //确认充值
                                //DateTime dt = DateTime.Now;
                                string msg = this.cardReader.Recharge(handle,mac2);
                                
                                //获取电子钱包余额
                                strBalence = this.cardReader.GetBalance(handle);
                                this.cardReader.LEDDisplay(handle, strBalence, 4000);
                                break;
                            }
                            else
                            {
                                Microsoft.Windows.Controls.MessageBox.Show("激活卡失败！", "充值");
                            }
                        }
                        else
                        {
                            flag = true;
                            Microsoft.Windows.Controls.MessageBox.Show("请放上卡！", "充值");
                        }
                    }
                }
                //关闭读卡器
                this.cardReader.CloseReader(handle);
            }
            catch(Exception ex)
            {
                this.cardReader.CloseReader(handle);
            }
        }
    }
}
